import router from './router'
import store from './store'
import { getPermissions } from './api/login'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
const whiteList = ['/login', '/platform']

NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  if (
    whiteList.indexOf(to.path) === -1 &&
    store.getters.addRouters.length === 0
  ) {
    getPermissions().then(({ data: { data } }) => {
      store.dispatch('setPermission', data).then(_ => {
        router.addRoutes(store.getters.addRouters)
        next({ ...to, replace: true })
      })
    })
  } else {
    next()
  }
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})
